package notentool;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;


@SuppressWarnings("serial")
public class PopupLogin extends JFrame {
	private JTextField tfMatrikel;
	private JPasswordField passwordField;
	JButton btnAbbrechen;
	JButton btnSpeichern;
	public JCheckBox chckbxSaveNoLogin;
	JLabel labelMatrikel;
	public boolean saveNoLogin;
	
	private JPanel panelButtons;
		
	public PopupLogin(String title) {
		constructor(title, null);
	}
	
	/* requires a checkbox from the calling view. give PopupSaveFile("TITEL", null) to create a local */
	public PopupLogin(String title,  JCheckBox saveNoLogin_ref){
		constructor( title, saveNoLogin_ref);		
	}
	
	/* necessary to overload the real constructor without redundant code */
	void constructor(String title, final JCheckBox saveNoLogin_ref){
		setForeground(Color.BLACK);
		setTitle("Logindaten");
		setSize(new Dimension(344, 140));
		
		getContentPane().setLayout(new BorderLayout(0, 0));
		
		JPanel panelData = new JPanel();
		getContentPane().add(panelData, BorderLayout.CENTER);
		GridBagLayout gbl_panelData = new GridBagLayout();
		gbl_panelData.columnWeights = new double[]{0.0, 1.0};
		gbl_panelData.rowWeights = new double[]{0.0, 0.0};
		panelData.setLayout(gbl_panelData);
		
		
		labelMatrikel = new JLabel("Matrikelnr.:");
		labelMatrikel.setForeground(Color.BLACK);
		labelMatrikel.setHorizontalAlignment(JLabel.RIGHT);
		GridBagConstraints gbc_labelMatrikel = new GridBagConstraints();
		gbc_labelMatrikel.fill = GridBagConstraints.BOTH;
		gbc_labelMatrikel.insets = new Insets(0, 0, 5, 5);
		gbc_labelMatrikel.gridx = 0;
		gbc_labelMatrikel.gridy = 0;
		panelData.add(labelMatrikel, gbc_labelMatrikel);
		
		tfMatrikel = new JTextField();
		GridBagConstraints gbc_tfMatrikel = new GridBagConstraints();
		gbc_tfMatrikel.insets = new Insets(0, 0, 5, 0);
		gbc_tfMatrikel.fill = GridBagConstraints.BOTH;
		gbc_tfMatrikel.gridx = 1;
		gbc_tfMatrikel.gridy = 0;
		panelData.add(tfMatrikel, gbc_tfMatrikel);
		tfMatrikel.setColumns(6);
		
		JLabel labelPW = new JLabel("Passwort:");
		labelPW.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_labelPW = new GridBagConstraints();
		gbc_labelPW.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelPW.insets = new Insets(0, 0, 5, 5);
		gbc_labelPW.gridx = 0;
		gbc_labelPW.gridy = 1;
		panelData.add(labelPW, gbc_labelPW);
		
		passwordField = new JPasswordField();
		GridBagConstraints gbc_passwordField = new GridBagConstraints();
		gbc_passwordField.insets = new Insets(0, 0, 5, 0);
		gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
		gbc_passwordField.gridx = 1;
		gbc_passwordField.gridy = 1;
		panelData.add(passwordField, gbc_passwordField);
		
		JPanel panelSaveLogin = new JPanel();
		getContentPane().add(panelSaveLogin, BorderLayout.SOUTH);
		panelSaveLogin.setLayout(new BorderLayout(0, 3));
		
	
		chckbxSaveNoLogin = new JCheckBox("Logindaten beim Speichern auslassen");
		chckbxSaveNoLogin.setToolTipText("Passwort und Matrikelnummer werden beim Speichern nicht mit in deiner Noten.xml Datei gespeichert.");
		chckbxSaveNoLogin.setHorizontalAlignment(SwingConstants.CENTER);
		chckbxSaveNoLogin.setSelected( (null == saveNoLogin_ref)?false:saveNoLogin_ref.isSelected() );
		
		 /* changes the saveNoLogin reference on checkbox item change 
		  * and set the checkbox property "selected"
		  * */
		chckbxSaveNoLogin.addItemListener( new ItemListener() {
			public void itemStateChanged(ItemEvent ie) {
				/* ask for chckbxSaveNoLogin doesn't work. i don't no why.*/
				chckbxSaveNoLogin.setSelected( (ItemEvent.SELECTED  == ie.getStateChange())? true : false );
				
				////System.out.println("checked " + chckbxSaveNoLogin.isSelected() + " " + ie.getStateChange() );
				
				/* return the checkbox status if the reference isn't null */
				if( null != saveNoLogin_ref )
					saveNoLogin_ref.setSelected( chckbxSaveNoLogin.isSelected() );
			}
		});
		
		
		panelSaveLogin.add(chckbxSaveNoLogin, BorderLayout.NORTH);
				
		panelButtons = new JPanel();
		panelSaveLogin.add(panelButtons, BorderLayout.SOUTH);
		panelButtons.setLayout(new GridLayout(1, 0, 0, 10));
		
		btnSpeichern = new JButton("OK");
		panelButtons.add(btnSpeichern);
		
		btnAbbrechen = new JButton("Abbrechen");
		panelButtons.add(btnAbbrechen);
	}
	
	void setPassword( String pw ){
		if( null != passwordField )
			passwordField.setText(pw);
	}
	/* return the text from the password field */
	String getPassword(){
		return String.valueOf( this.passwordField.getPassword() );
	}
	
	void setMatrikel( String mat ){
		this.tfMatrikel.setText(mat);
	}
	
	/* returns the matrikel number 
	 * or -1 on failure 
	 * or 0 if the matrikelnumber was empty*/
	int getMatrikel(){
		int mat=-1;
		if( 0 == tfMatrikel.getText().trim().length() )
			return 0;
		try{
			mat = Integer.parseInt(tfMatrikel.getText().trim());
			// code if the matrikelnumber is a valid int. ------------------------~
			resetFailureMsg();
			// --------------------------------------------------------------------~
		}catch( NumberFormatException e){
			mat = -1; // don't close the popup
			//sv1.popLogin.lMatrikelError.setText(
			
			this.setTitle(
				this.tfMatrikel.getText()
					+ " ist keine Ganzahl!"
			);
			this.setForeground(Color.RED);
			this.labelMatrikel.setForeground(Color.RED);
		}
		
		return mat;
	}
	
	void resetFailureMsg(){
		this.setTitle("Logindaten");
		this.setForeground(Color.BLACK);
		this.labelMatrikel.setForeground(Color.BLACK);
	}
}
